package com.frinder.controller;

import com.frinder.entity.DataTables;
import com.frinder.entity.Response;
import com.frinder.entity.Student;
import com.frinder.entity.SubStudent;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by kiktech on 2017/5/5.
 */
@RestController
@RequestMapping("/datatables")
public class DataTablesController {

    public static List<Student> studentList = new ArrayList() {{
        Student student;
        for (int i = 1; i <= 1000; i++) {
            int index = i;
            student = new Student();
            student.setId(i);
            student.setName("name" + index);
            student.setAge(27 + index % 9);
            add(student);
        }
    }};

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Student> list() {
        return studentList;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public DataTables<Student> page(DataTables<Student> entity) throws Exception {
        Comparator<Student> comparator;
//        if ("id asc".equalsIgnoreCase(entity.getOrderBy())) {
//            comparator = Comparator.comparingLong(Student::getId);
//        } else {
//            comparator = Comparator.comparingLong(Student::getId).reversed();
//        }
        if ("name asc".equalsIgnoreCase(entity.getOrderBy())) {
            comparator = Comparator.comparing(Student::getName);
        } else {
            comparator = Comparator.comparing(Student::getName).reversed();
        }
        Student student = entity.getT(Student.class);
        List<Student> searchList = studentList.parallelStream()
                .filter(
                        s ->
                                (StringUtils.isEmpty(student.getSearchText()) ? true : s.getName().startsWith(student.getSearchText()))
                                        && (StringUtils.isEmpty(student.getName()) ? true : s.getName().startsWith(student.getName()))
                                        && (student.getAge() > 0 ? s.getAge() == student.getAge() : true)
                )
                .sorted(comparator)
                .collect(Collectors.toList());
        int total = searchList.size();
        int start = entity.getStart();
        String pageNum = entity.getPageNum();
        int len = entity.getLength();
        if (!StringUtils.isEmpty(pageNum)) {
            int page = Integer.parseInt(pageNum);
            start = (page > 0 ? (page - 1) : 0) * len;
        }
        start = start < 0 ? 0 : start;
        start = start > total ? 0 : start;
        int to = start + len;
        to = (total >= to) ? to : total;
        return entity.getResponse(total, total, searchList.subList(start, to));
    }

    @RequestMapping("/remove")
    public Response remove(SubStudent student) {
        List<Long> ids = student.getIds();
        List<Student> subList = studentList.parallelStream()
                .filter(s -> !ids.contains(s.getId()))
                .collect(Collectors.toList());
        studentList.clear();
        studentList.addAll(subList);
        return Response.SUCCESS;
    }

    @RequestMapping("/add")
    public String persist(Student student) {
        studentList.add(student);
        return Response.Code.SUCCESS.getMessage();
    }

    @RequestMapping("/add2")
    public Response insert(Student student) {
        studentList.add(student);
        return Response.SUCCESS;
    }


}
